﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Threading;
using Hammock;
using Hammock.Web;
using HtmlAgilityPack;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;
using _1906Ultras.Model;

namespace _1906Ultras.Helpers
{
    public class PhotoHelper : HelperBase
    {

        public PhotoHelper()
        {
            Authority = "http://www.1906ultras.com/";
            Request.Path = "gallery";
        }

        public void GetPhotos(Action<ObservableCollection<Photo>> callback)
        {

            BeginRequest((response) =>
            {
                try
                {
                    HtmlDocument htmDoc = new HtmlDocument();
                    htmDoc.LoadHtml(response);
                    var node =
                        htmDoc.DocumentNode.SelectNodes(
                            "//div[@class='ngg-gallery-thumbnail-box']//a[@class='shutterset_set_2']");

                    var photos = new ObservableCollection<Photo>();
                    foreach (var imgNode in node)
                    {
                        var img = imgNode.ChildNodes["img"];
                        var p = new Photo()
                                    {
                                        PhotoLink = imgNode.Attributes["href"].Value,
                                        ThumbLink = img.Attributes["src"].Value
                                    };
                        photos.Add(p);

                    }


                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                                              {
                                                                  GlobalLoading.Instance.IsLoading = false;
                                                                  callback(photos);
                                                              });
                }
                catch (Exception e)
                {
                    ErrorCallbackInternal(e);
                }


            });
         
            
        }

    }
}
